package com.providersave.contoller;

import com.providersave.service.StockInfoService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import pojo.StockInfo;
import tools.PageBean;
import tools.Result;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class StockInfoController {
    @Resource
    private StockInfoService stockInfoService;

    //添加库存信息请求
    @RequestMapping("/addStockInfo")
    public String addStockInfo(@RequestBody StockInfo stockInfo){
        Result result = new Result(500,"添加失败");
        if(stockInfoService.addStockInfo(stockInfo)){
            result.setCode(200);
            result.setMsg("添加成功");
        }
        return result.toString();
    }

    //删除库存信息请求
    @RequestMapping("/removeStockInfo")
    public String removeStockInfo(@RequestParam("id")Long id){
        Result result = new Result(500,"删除失败");
        if(stockInfoService.removeStockInfo(id)){
            result.setCode(200);
            result.setMsg("删除成功");
        }
        return result.toString();
    }

    //修改库存信息请求
    @RequestMapping("/modifyStockInfo")
    public String modifyStockInfo(@RequestBody StockInfo stockInfo){
        Result result = new Result(500,"修改失败");
        if(stockInfoService.modifyStockInfo(stockInfo)){
            result.setCode(200);
            result.setMsg("修改成功");
        }
        return result.toString();
    }

    //获取库存信息分页集合请求
    @RequestMapping("/getStockInfoPageList")
    public String getWarehousePageList(@RequestBody(required = false) StockInfo stockInfo,
                                       @RequestParam("pageIndex") Integer pageIndex,
                                       @RequestParam("pageSize") Integer pageSize){
        Integer totalCount = stockInfoService.findStockInfoCountByCondition(stockInfo);
        PageBean stockInfoPage = new PageBean(pageIndex,pageSize,totalCount);

        List<StockInfo> stockInfoList = stockInfoService.findStockInfoPagingByCondition(stockInfo,pageIndex,pageSize);
        stockInfoPage.setList(stockInfoList);

        Result result = new Result(200,"查询成功",stockInfoPage);
        return result.toString();
    }

    //获取可用库存信息分页集合请求
    @RequestMapping("/getUsableStockInfoPaging")
    public String getUsableStockInfoPaging(@RequestParam("pageIndex") Integer pageIndex,
                                           @RequestParam("pageSize") Integer pageSize){
        Integer totalCount = stockInfoService.findUsableStockInfoCount();
        PageBean stockInfoPage = new PageBean(pageIndex,pageSize,totalCount);

        List<StockInfo> stockInfoList = stockInfoService.findUsableStockInfoPaging(pageIndex,pageSize);
        stockInfoPage.setList(stockInfoList);

        Result result = new Result(200,"查询成功",stockInfoPage);
        return result.toString();
    }

    //获取包含指定商品的库存信息集合
    @RequestMapping("/getStockInfoStockInfoByGoodsId")
    public String getStockInfoStockInfoByGoodsId(@RequestParam("goodsId") Long goodsId){
        List<StockInfo> stockInfoList = stockInfoService.findStockInfoByGoodsId(goodsId);

        Result result = new Result(200,"查询成功",stockInfoList);
        return result.toString();
    }

    //获取包含指定仓库和商品的库存信息
    @RequestMapping("/getStockInfoByWarehouseAndGoods")
    public String getStockInfoByWarehouseAndGoods(@RequestParam("warehouseId") Long warehouseId,@RequestParam("goodsId") Long goodsId){
        StockInfo stockInfo = stockInfoService.findStockInfoByWarehouseAndGoods(warehouseId,goodsId);

        Result result = new Result(200,"查询成功",stockInfo);
        return result.toString();
    }

}
